from common.cp.maestro.key_mapper.base_event_key_mapper import get_base_event_key_mapper
from event_rule.cp.base_event_rule.rule_config import RULE_CONFIG
from common.cp.maestro.key_mapper.op_event_key_mapper import KEY_MAPPER_FUNC as OP_EVENT_KEY_MAPPER_FUNC
from common.cp.maestro.key_mapper.product_event_key_mapper import KEY_MAPPER_FUNC as PRODUCT_EVENT_KEY_MAPPER_FUNC
from common.cp.maestro.key_mapper.user_event_key_mapper import KEY_MAPPER_FUNC as USER_EVENT_KEY_MAPPER_FUNC


KEY_MAPPER_FUNC = OP_EVENT_KEY_MAPPER_FUNC
KEY_MAPPER_FUNC.update(PRODUCT_EVENT_KEY_MAPPER_FUNC)
KEY_MAPPER_FUNC.update(USER_EVENT_KEY_MAPPER_FUNC)


def get_key_mapper(logical_page_id, index, **kwargs):
    # 基礎事件約定logical_page_id為event_id
    try:
        int(logical_page_id)
        assert logical_page_id in KEY_MAPPER_FUNC
        func = KEY_MAPPER_FUNC.get(logical_page_id)
    except ValueError:
        event_id = logical_page_id
        event_rule = RULE_CONFIG.get(event_id)
        kwargs['event_rule'] = event_rule
        func = get_base_event_key_mapper
    key_mapper = func(**kwargs)
    return key_mapper
